package com.wzq.design.business.system.menu.service;

import cn.hutool.core.util.RadixUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzq.design.base.dto.QueryByIdRequest;
import com.wzq.design.base.service.CommService;
import com.wzq.design.business.system.menu.dto.RouteDto;
import com.wzq.design.business.system.menu.dto.SysMenuDto;
import com.wzq.design.business.system.menu.entity.SysMenu;
import com.wzq.design.business.system.menu.mapper.MenuMapper;
import com.wzq.ssm.parent.util.EntityUtil;
import com.wzq.ssm.parent.util.TreeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.hutool.core.util.RadixUtil.RADIXS_59;

/**
 * @author wangzhiqiang
 * @since 2023/5/8 15:05
 */
@Service
public class MenuService extends CommService<MenuMapper, SysMenu> {

    private final MenuMapper menuMapper;


    public MenuService(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Transactional
    public SysMenuDto save(SysMenuDto dto) {
        SysMenu sysMenu;
        if (StringUtils.isBlank(dto.getId())) {
            sysMenu = EntityUtil.castDtoToModel(dto, SysMenu.class);
            setParentIds(sysMenu);
            menuMapper.insert(sysMenu);
        } else {
            sysMenu = menuMapper.selectById(dto.getId());
            String oldParentId = sysMenu.getParentId();
            EntityUtil.copyDtoToBean(dto, sysMenu);
            if (!Objects.equals(oldParentId, dto.getParentId())) {
                setParentIds(sysMenu);
            }
            menuMapper.updateById(sysMenu);
        }

        return SysMenuDto.fromDb(sysMenu);
    }


    public List<SysMenuDto> list() {
        List<SysMenu> sysMenuList = menuMapper.list();
        return sysMenuList.stream().map(SysMenuDto::fromDb).collect(Collectors.toList());
    }

    public List<SysMenuDto> listTree() {
        List<SysMenu> sysMenuList = menuMapper.list();
        return TreeUtil.build(sysMenuList.stream().map(SysMenuDto::fromDb).collect(Collectors.toList()));
    }


    public SysMenuDto findById(QueryByIdRequest userRequest) {
        SysMenu sysMenu = menuMapper.selectById(userRequest.getId());
        return SysMenuDto.fromDb(sysMenu);
    }

    public List<RouteDto> getMenuTree(){
        QueryWrapper<SysMenu> query=new QueryWrapper<>();
        query.eq("visible",1).in("type", Arrays.asList(SysMenu.MenuTypeEnum.MENU, SysMenu.MenuTypeEnum.CATALOG, SysMenu.MenuTypeEnum.EXTLINK));
        query.orderByAsc("sort");
        List<SysMenu> baseMenus = menuMapper.selectList(query);
        if (CollectionUtils.isEmpty(baseMenus)){
            return Collections.emptyList();
        }

        List<RouteDto> baseMenuDtos=baseMenus.stream().map(RouteDto::fromDbo).collect(Collectors.toList());
        return TreeUtil.build(baseMenuDtos);
    }


    public static void main(String[] args) {
        long a=3541354345555525121L;
        String encode = RadixUtil.encode(RADIXS_59,3541354345555525121L);

        System.out.println(encode);


    }
}
